/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rejeev.tracer;

import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

/**
 *
 * @author rejeev.divakaran
 */
public class Configuration {
    private Properties props = new Properties();
    private static Configuration singleton = new Configuration();
    private Set<String> excludeList;
    private Set<String> includeList;
    private boolean allThreadsLogged;
    private boolean loggingOn;
    private Set<String> threadList = new HashSet<String>();
    private boolean file;
    private boolean console;
    
    private Configuration(){
        try{
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("tracer.properties");
            props.load(is);
            String excludedPackages = props.getProperty("excludedPackages");
            if(excludedPackages != null) excludeList = parse(excludedPackages);
            else excludeList = new HashSet<String>();
            String allThreadsString = props.getProperty("allThreadsLogged");
            if(allThreadsString != null && allThreadsString.equals("true")) allThreadsLogged = true;
            String includedPackages = props.getProperty("includedPackages");
            if(includedPackages != null) includeList = parse(includedPackages);
            String threadListString = props.getProperty("threadList");
            if(threadListString != null) threadList = parse(threadListString);
            String loggingOnString = props.getProperty("loggingOnAtStartup");
            if(loggingOnString != null && loggingOnString.equals("true")) loggingOn = true;
            String fileString = props.getProperty("file");
            if(fileString != null && fileString.equals("true")) file = true;
            String consoleString = props.getProperty("console");
            if(consoleString != null && consoleString.equals("true")) console = true;
            
            System.out.println(excludeList);
            System.out.println("loggingON: " + loggingOn);
            System.out.println("all threads: " + allThreadsLogged);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static Configuration getInstance(){
        return singleton;
    }
    public boolean isToBeExcluded(String className){
        //if(includeList != null && !includeList.contains(className)) return false;
        if(includeList != null){
            boolean contains = false;
            for(String pkg : includeList){
                if(className.startsWith(pkg)) {
                    contains = true;
                    break;
                }
            }
            if(!contains) return true;
        }
        //if(excludeList.start(className)) return true;
        for(String pkg : excludeList){
            if(className.startsWith(pkg)) return true;
        }
        return false;
    }
    
    public boolean isLoggingEnabled(){
        if(!loggingOn) return false;
        if(allThreadsLogged) return true;
        if(threadList.contains(Thread.currentThread().getName())) return true;
        return false;
    }
    
    public boolean isFile(){
        return file;
    }
    
    public boolean isConsole(){
        return console;
    }
    
    public void addThread(String threadName){
        threadList.add(threadName);
    }
    
    public void removeThread(String threadName){
        threadList.remove(threadName);
    }
    
    public void enableLogging(boolean value){
        loggingOn = value;
    }

    public void setAllThreadsLogged(boolean flag){
        allThreadsLogged = flag;
    }
    
    private Set<String> parse(String str){
        StringTokenizer tokens = new StringTokenizer(str, ",");
        Set<String> returnValue = new HashSet<String>();
        while(tokens.hasMoreTokens()){
            returnValue.add(tokens.nextToken());
        }
        return returnValue;
    }
}
